package ru.ipo.dces.plugins.admin.components;

import com.l2fprod.common.swing.renderer.DefaultCellRenderer;
import ru.ipo.dces.plugins.admin.beans.ContestsListBean;
import ru.ipo.dces.plugins.admin.resources.ImageResources;

import javax.swing.*;
import java.awt.*;
import java.util.Date;

public class IconListRenderer extends DefaultCellRenderer {

    public static final Color CONTEST_HAS_NOT_BEGAN = Color.GREEN;
    public static final Color CONTEST_FINISHED = Color.RED;
    public static final Color CONTEST_IS_NOW = Color.ORANGE;

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                                                  boolean cellHasFocus) {

        JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected,
                cellHasFocus);

        Icon icon = null;
        ContestsListBean bean = (ContestsListBean) value;

        if (bean.getDescription().registrationType != null) {
            switch (bean.getDescription().registrationType) {
                case ByAdmins:
                    icon = ImageResources.ADMIN_REGISTRATION_IMAGE;
                    break;
                case Self:
                    icon = ImageResources.SELF_REGISTRATION_IMAGE;
                    break;
                default:
            }

            Date currentTime = new Date();
            if (bean.getDescription().finish.compareTo(currentTime) < 0)
                label.setForeground(CONTEST_FINISHED);
            else if (bean.getDescription().start.compareTo(currentTime) > 0)
                label.setForeground(CONTEST_HAS_NOT_BEGAN);
            else
                label.setForeground(CONTEST_IS_NOW);
        }

        if (bean.getDescription().contestID == 0)
            label.setForeground(Color.GRAY);

        if (icon != null)
            label.setIcon(icon);
        return label;
    }
}
